﻿using Model;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Inventory
{
    public partial class login : System.Web.UI.Page
    {
        private BLL.SellerBll _bll = new BLL.SellerBll();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["action"] == "checkin") {
                CheckIn();
            }
            else if (Request["action"] == "signin")
            {
                SignIn();
            }
            else if (Request["action"] == "passwordreset")
            {
                PwdReset();
            }
            else if (Request["action"] == "getvalidatecode")
            {
                GetValidateCode();
            }
            else if (Request["action"] == "checkemail")
            {
                CheckEmail();
            }
            else if (Request["action"] == "checkusername")
            {
                CheckUserName();
            }
            else if (Request["action"] == "checkverifycode")
            {
                CheckVerifyCode();
            }

        }

        /// <summary>
        /// 登录
        /// </summary>
        private void CheckIn() {
            string userName = Request["_username"];
            string password = Request["_password"];
            string returnPage = Request["_returnPage"];

            Seller model = _bll.ValidateLogin(userName, password);

            if (model == null)
            {
                Response.Write("Error");
                Response.End();
            }
            else
            {
                Session["CurrentUser"] = model;
                Response.Redirect("/index.aspx");
            }
            
        }

        /// <summary>
        /// 注册
        /// </summary>
        private void SignIn()
        {
            string email = Request["Email"];
            string username = Request["UserName"];
            string nickname = Request["NickName"];
            string type = Request["type"];
            string shopName = Request["shopName"];
            string ShopUrl = Request["ShopUrl"];
            string pwd = Request["pwd"];
            string repwd = Request["repwd"];
            string Tel = Request["Tel"];
            string Aliww = Request["Aliww"];
            string verifyCode = Request["verifyCode"];


            Seller model = new Seller();

            model.Email = email;
            model.UserName = username;
            model.NickName = string.IsNullOrEmpty(nickname) ? username : nickname;
            model.ShopName = shopName;
            model.Type = int.Parse(type);
            model.ShopUrl = ShopUrl;
            model.Tel = Tel;
            model.Pwd = pwd;
            model.Aliww = Aliww;
            model.AddTime = DateTime.Now;
            
            try
            {
                _bll.Add(model);
            }
            catch(Exception e)
            {
                Response.Write(e.ToString());
                Response.End();
            }
            Response.Redirect("/index.aspx");
        }

        /// <summary>
        /// 找回密码
        /// </summary>
        private void PwdReset()
        { 

        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        private void GetValidateCode()
        {
            //生成验证码
            ValidateCode validateCodeGen = new ValidateCode();
            string code = validateCodeGen.CreateValidateCode(5);
            Session["validatecode"] = code;
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AppendHeader("Pragma", "No-Cache");
            Response.Cache.SetNoStore(); //这一句 		
            Response.ClearContent();
            Response.ContentType = "image/Jpeg";
            Response.BinaryWrite(validateCodeGen.CreateValidateGraphic(code));
            Response.End();
        }

        /// <summary>
        /// 检查该Email是否已存在
        /// </summary>
        private void CheckEmail()
        {
            string email = Request["email"];
            bool bRet = !_bll.EmailExisted(email);  //取反
            Response.Write(bRet.ToString().ToLower());
            Response.End();
        }

        /// <summary>
        /// 检查该用户名是否已存在
        /// </summary>
        private void CheckUserName()
        {
            string username = Request["username"];
            bool bRet = !_bll.UserNameExisted(username);  //取反
            Response.Write(bRet.ToString().ToLower());
            Response.End();
        }

        /// <summary>
        /// 检查验证码
        /// </summary>
        private void CheckVerifyCode()
        {
            string code = Request["verifyCode"];
            bool bRet = (Session["validatecode"].ToString().ToLower() == code.ToLower());
            Response.Write(bRet.ToString().ToLower());
            Response.End();
        }
    }
}